//
//  MNDInvestDetailViewController.m
//  minidai
//
//  Created by Vitas on 2018/9/1.
//  Copyright © 2018年 迷你贷. All rights reserved.
//

#import "MNDInvestDetailViewController.h"
#import "SYRefreshControl.h"
#import "MNDInvestDetailModel.h"
#import "MNDInvestFriendCell.h"

#define kBottomViewHeight 111

@interface MNDInvestDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(assign,nonatomic) NSInteger pageNum;
//模型数组
@property(strong,nonatomic) NSMutableArray *investListModelArray;
@property (strong, nonatomic)UIView *placeHolderMaskView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UILabel *rewardRuleLabel;
//邀请好友人数
@property (weak, nonatomic) IBOutlet UILabel *inviteFriendNumLabel;
//累计奖励
@property (weak, nonatomic) IBOutlet UILabel *totalAwardMoneyLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomContainerViewHeight;
//出借金额文字标题
@property (weak, nonatomic) IBOutlet UILabel *borrowMoneyTipLabel;
//奖励文字标题
@property (weak, nonatomic) IBOutlet UILabel *awardLabel;
//好友邀请人数或累计撮合金额
@property (weak, nonatomic) IBOutlet UILabel *firstTipLabel;
//头部视图
@property (weak, nonatomic) IBOutlet UIView *topView;
//头部高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewHeight;


@end

@implementation MNDInvestDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44;
    // Do any additional setup after loading the view.
}

#pragma mark - 懒加载
- (NSMutableArray *)investListModelArray {
    if (!_investListModelArray) {
        _investListModelArray = [NSMutableArray array];
    }
    return _investListModelArray;
}

#pragma mark -搭建界面
- (void)setupUI {
    
    if (self.index == 1) {
        self.title = @"撮合奖励详情";
        self.firstTipLabel.text = @"累计撮合金额(元)";
        self.bottomContainerViewHeight.constant = kBottomViewHeight;
        
        
    }else if(self.index == 2){
        self.title = @"邀请奖励详情";
        self.bottomContainerViewHeight.constant = 0;
        self.bottomView.hidden = YES;
        self.awardLabel.text = @"邀请奖励";
        self.borrowMoneyTipLabel.text = @"注册日期";
    }else {
        self.topView.hidden = YES;
        self.topViewHeight.constant = 0;
        self.title = @"好友邀请";
        self.bottomContainerViewHeight.constant = 0;
        self.bottomView.hidden = YES;
        self.awardLabel.text = @"邀请奖励";
        self.borrowMoneyTipLabel.text = @"注册日期";
        self.bottomView.hidden = YES;

    };
    [self creatMaskView];
    self.tableView.tableFooterView = [UIView new];
    self.bottomView.layer.cornerRadius = 4.0f;
    self.bottomView.layer.borderColor = [UIColor colorWithRGB:0xF1F1F1].CGColor;
    self.bottomView.layer.borderWidth = 1.0f;
    WeakSelf(self);
    
    [SYRefreshControl configGifMJRefresh:self.tableView refreshType:MJRefreshSupportALl beginRefresh:YES timeLabelHidden:YES stateLabelHidden:YES refreshBlock:^{

        [weakself loadInvestListData:YES];

    } loadMoreBlock:^{
 
        [weakself loadInvestListData:NO];

    }];
}

#pragma mark -邀请数据
- (void)loadInvestListData:(BOOL)isPullDown {
    NSString *perpage = @"1";
    __weak typeof (self) weakSelf =  self;
    //    BOOL isPullDown = [self.refreshHeader isRefreshing];
    //    [NetworkProgressHUD showHud];
    if (isPullDown) {
        self.pageNum = 1;
    }else {
        self.pageNum++;
    }
    perpage = [NSString stringWithFormat:@"%zd",self.pageNum];
    
    [APIManager sendRequestWithPath:API_MAKE_REWARD_DETAIL parameters:[MNDSign signedParamWithDict:@{@"perpage":perpage,@"pagesize":@"10",@"reward_type":@(self.index)}] method:kXMHTTPMethodGET onSuccess:^(ResponseModel *responseModel) {
        
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD hideHud];
            MNDInvestDetailModel *detailModel = [MNDInvestDetailModel mj_objectWithKeyValues:responseModel.data];
            NSArray *modelArray = detailModel.reward_list;
            
            if (isPullDown) {
                [weakSelf.investListModelArray removeAllObjects];
            }
            [weakSelf.investListModelArray addObjectsFromArray:modelArray];
            if (weakSelf.investListModelArray.count <= 0) {
                weakSelf.placeHolderMaskView.hidden = NO;
            }else {
                weakSelf.placeHolderMaskView.hidden = YES;
            }
            
            weakSelf.rewardRuleLabel.text = detailModel.page_describe;
            weakSelf.totalAwardMoneyLabel.text = detailModel.make_reward;
            if(self.index == 1) {
                weakSelf.inviteFriendNumLabel.text = detailModel.invite_tender_money;
            }else{
                weakSelf.inviteFriendNumLabel.text = [NSString stringWithFormat:@"%zd",detailModel.sum];
            }
                
            
            [SYRefreshControl configAfterLoading:weakSelf.tableView hasData:modelArray.count];
        } else {
            NSDictionary *dict = responseModel.msg;
            if (dict && dict.count > 0) {
                [NetworkProgressHUD showHudMsg:dict[@"error"]];
            } else {
                [NetworkProgressHUD showHudMsg:@"获取数据失败"];
            }
            [SYRefreshControl configAfterLoading:weakSelf.tableView hasData:NO];
        }
        [weakSelf.tableView reloadData];
        
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
        [SYRefreshControl configAfterLoading:weakSelf.tableView hasData:NO];
    }];
}


#pragma mark -UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.investListModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MNDInvestListModel *investModel = self.investListModelArray[indexPath.row];
    
    MNDInvestFriendCell *investListCell = [tableView dequeueReusableCellWithIdentifier:@"MNDInvestFriendCell"];

    if (self.index == 1) {
        investListCell.inviteAddTimeLabel.hidden = NO;
        investListCell.timeTopMargin.constant = 8;
        investListCell.inviteUserNameLabel.text = investModel.invite_user_name;
        investListCell.inviteAddTimeLabel.text = [NSString stringWithFormat:@"%@注册",investModel.invite_user_regtime];
        investListCell.inviteIncomeLabel.text = investModel.reward_income;
        investListCell.borrowMoneyLabel.text = investModel.tender_money;
    }else {
        investListCell.inviteAddTimeLabel.hidden = YES;
        investListCell.timeTopMargin.constant = -10;
        investListCell.inviteUserNameLabel.text = investModel.invite_user_name;
        investListCell.inviteIncomeLabel.text = investModel.reward_income;
        investListCell.borrowMoneyLabel.text = investModel.invite_user_regtime;
    }

    return investListCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark - 无数据时显示图片
- (void)creatMaskView {
    UIView *maskView = [[UIView alloc]initWithFrame:self.view.bounds];
    maskView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
    //    [self.view insertSubview:maskView aboveSubview:self.tableView];
    [self.tableView addSubview:maskView];
    UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"financen_detail_data_empty"]];
    UILabel *tipLabel = [[UILabel alloc]init];
    tipLabel.text = @"哎呀~这里空空如也~";
    tipLabel.font = [UIFont systemFontOfSize:14];
    tipLabel.textColor = kColorGray;
    
    [maskView addSubview:tipLabel];
    [maskView addSubview:imgV];
    
    imgV.layer.cornerRadius = imgV.bounds.size.height*0.5;
    imgV.clipsToBounds = YES;
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(maskView);
        make.centerY.equalTo(maskView).mas_offset(-180);
    }];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imgV);
        make.top.equalTo(imgV.mas_bottom).offset(10);
    }];
    
    self.placeHolderMaskView = maskView;
    self.placeHolderMaskView.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
